package com.yxjme.redrainapp;

import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import com.yxjme.redrainlibrary.a.CountDownUtil;
import com.yxjme.redrainlibrary.a.WindowManagerPermission;
import com.yxjme.redrainlibrary.m.RedPacket;
import com.yxjme.redrainlibrary.v.ShowRedRainView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ShowRedRainView mShowRedRainView ;
    private TextView tv_time;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >=19 && Build.VERSION.SDK_INT < 21) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            viewStatueHeight.setVisibility(View.VISIBLE);
//            viewStatueHeight.setMinimumHeight(Utils.getStatusBarHeight(this));
        }
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();

            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
//                    (t==1?View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR:View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        initView();
    }


    private void initView() {
        View view = LayoutInflater.from(this).inflate(R.layout.redpacket_layout,null);
        RedPacketRainWindowManager.newInstance(this).setContentView(view);
        RedPacketRainWindowManager.newInstance(this).show();
        int[] d ={R.mipmap.icon_gold_rain_1,
                R.mipmap.icon_gold_rain_2,
                R.mipmap.icon_gold_rain_3,
                R.mipmap.icon_gold_rain_4,
                R.mipmap.icon_gold_rain_5,
                R.mipmap.icon_gold_rain_6,};

        mShowRedRainView=view.findViewById(R.id.mShowRedRainView);
        mShowRedRainView.setDrawables(d);
        mShowRedRainView.setOnAddClickListener(listener);
        mShowRedRainView.setRedPacketDownSeep(15);
        mShowRedRainView.setRedPacketSize(0.9f);
        mShowRedRainView.setDuration(5000);
        view.findViewById(R.id.launchBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShowRedRainView.setData(initList());
                mShowRedRainView.start();
            }
        });

        view.findViewById(R.id.reStart).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShowRedRainView.reset();
            }
        });

        tv_time =view. findViewById(R.id.tv_time);
        Button button=new Button(this);
        button.setText("打开了地方sad麻烦");
    }


    @Override
    protected void onResume() {
        super.onResume();
        RedPacketRainWindowManager.newInstance(this).resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        RedPacketRainWindowManager.newInstance(this).pause();
    }


    /**
     * @return
     */
    public List<String> initList(){
        List<String> list  = new ArrayList<>() ;
        for (int i = 0 ; i < 100 ; i++){
            list.add(""+i);
        }
        return list ;
    }




    /**
     * 显示同时只能有5个播放的声音
     */
    private List<MediaPlayer> list = new ArrayList<>();


    /**
     * 抢金币回调
     */
    private ShowRedRainView.OnCurrentRedPacketListener listener = new ShowRedRainView.OnCurrentRedPacketListener() {

        /*抢金币*/
        @Override
        public void onCurrentRedPacket(RedPacket current, TextView textView, List<RedPacket> allRedPacket) {
            if (list.size()<=5){
                MediaPlayer mMediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.ring2);
                mMediaPlayer.start();
                list.add(mMediaPlayer);
                mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        list.remove(mediaPlayer);
                        mediaPlayer.stop();
                        mediaPlayer.release();
                    }
                });
            }
            textView.setText("+"+current.redInfo);
        }



        /*抢金币倒计时*/
        @Override
        public void onCountTime(final CountDownUtil.TimeBean timeBean) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_time.setText(timeBean.h+":"+timeBean.m+":"+timeBean.sc);
                }
            });
        }

        @Override
        public void onComplete() {

        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == WindowManagerPermission.REQUEST_CODE) {
            if (WindowManagerPermission.checkFloatPermission(this)){
                    RedPacketRainWindowManager.newInstance(this).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RedPacketRainWindowManager.newInstance(this).dismiss();
    }
}